\chapter{Theoretical background}
\label{ch:theory}

%\begin{figure}
%\centering
%\def\svgwidth{\columnwidth}
%\input{image.pdf_tex}
%\end{figure}

%%%% ***** RSSI and LQI 101 ****** %%%%
\section{RSSI and LQI 101}
RSSI and LQI are often used as measures for the wireless link quality. The RSSI (Radio Signal Strength Indicator) provides a measure of the signal strength at the receiver whereas the LQI (Link Quality Indicator) reflects the bit error rate of the connection. 

\begin{figure}[!]
  \centering
  \includegraphics[trim = 0mm 266mm 120mm 0mm, width=10cm]{rssi_coverage.pdf}
%  \includegraphics[width=10cm]{rssi_coverage.pdf}
  \caption{RSSI Coverage}
  \label{fig:rssi.coverage}
\end{figure}

The RSSI value is measured in dBm and express the signal power. The values typically range from -45dBm to -100dBm. The lower value is determined by the receiver input threshold, and the upper by the airborne signal strength. Circuit- and quantization noise is usually the limiting factor for the input receiver threshold. \\

The LQI value reflects the link quality seen from the receiver side. The LQI value correlates with the Packet Recption Rate (PRR) and is therefore a very important figure in mesh routing protcols \cite{Gomez:2010:ILR:1928820.1928826}. The value combines the RSSI value with a correlation of the expected and recieved data, thus being able to reflect a bad link quality in a noisy environment that results in a high RSSI value. There is no exact formulae for how to calculate the LQI value and the estimation method is implementation specific.
The LQI value ranges between 0 and 255 (802.15.4), where the highest value represents the maximum quality frames. 

LQI and RSSI values correlate to some extend. Without interference from other transmission medias and reflections, the LQI should remain stable in ``happy'' range of figure \ref{fig:rssi.coverage}, as RSSI drops in the ``not-so-happy'' range, the LQI should also drop as packet loss start occuring. Around the reciever threshold, the packet loss becomes so significant that the LQI value hits its lower boundary and remains there.

\begin{figure}[!]
  \centering
  \includegraphics[width=10cm]{theoretical_rssi_dist.png}
  \caption{Theoretical RSSI/LQI vs Distance}
  \label{fig:rssi.distance}
\end{figure}

Figure \ref{fig:rssi.distance} shows that LQI is not suitable for distance estimation. If the LQI value should be used for distance measurements, then it should only be within certain value boundaries. It could however be used to indicate a quality of the current RSSI value.

%%%% ***** Distance Measurement and Positioning with RSSI ****** %%%%
\section{Distance Measurement and Positioning with RSSI}
The power of an RF wave dampens along its way through a  given medium. Knowing the medium, it is possible to estimate the distance traveled, just from knowing the signal attenuation. For the ideal case, the distance versus RF signal attenuation is given by the Friis equation \cite{Friis46}:
\begin{equation}
\frac{P_{tx}}{ P_{rx}} = G_{tx}G_{rx}( \frac{\lambda}{4\pi R})^{2}
\end{equation}
The equation states that the difference in power, eg the power loss, is equal to the in- and output gain ($G_{tx} , G_{rx}$) multiplied by the wavelength, $\lambda$ divided by the distance R. The wavelength can also be expressed by the frequency: $\lambda=\frac{c}{f}$. \\
Rearranging this using the laws of logarithm, we get:\\
\begin{align*}
log(\frac{P_{tx}}{ P_{rx}})& = log(G_{tx}G_{rx}( \frac{\lambda}{4\pi R})^{2})\\
log(P_{tx})-log( P_{rx})& = log(G_{tx}G_{rx})+log(( \frac{\lambda}{4\pi R})^{2})\\
& = log(G_{tx})+log(G_{rx})+2log( \frac{c}{f})-2log(4\pi R)
\end{align*}

The path loss, PL is defined by:
\begin{equation}
PL=10log(P_{tx})-10log(P_{rx})\\
\end{equation}

Incorporating this in the previous equation we get:
\begin{align*}
10log(P_{tx})-10log( P_{rx})& = 10log(G_{tx})+10log(G_{rx})+20log( \frac{c}{f})-20log(4\pi R)\\
& = 10log(G_{tx})+10log(G_{rx})+20log( \frac{c}{4\pi f})-20log( R)
\end{align*}
Resulting in the equation:
\begin{equation}
PL_{open space}= 10log(G_{tx})+10log(G_{rx})+20log( \frac{c}{4\pi f})-20log( R)\\
\end{equation}

When we know the transmission power, the received power (RSSI) and the frequency (2.4GHz), we are able to estimate the distance. Distance function is logarithmic.

\begin{equation}
PL_{open space}= -20log( R) + A \\
\end{equation}
This can be modified to a general form, that take the transmission medium into account:
\begin{equation}
PL_{general}= -20 \* n \* log( R) + A \\
\end{equation}
Where n is derived empirically.

Knowing the distance to three anchors, allows us to estimate the current position using multilateration.
\begin{figure}[!]
  \centering
%trim option's parameter order: left bottom right top from the images lower left corner
% Tells how much to crop, NOT the size of the figure
  \includegraphics[trim = 0mm 212mm 132mm 0mm, width=8cm]{trilateration.pdf}
  \caption{Trilateration with three anchors}
  \label{fig:trilateration}
\end{figure}

Using Pythagoras' theorem, we can derive the coordinates of the mote.
\begin{eqnarray*}
(x_{anchor_1}- x_{mote})^2 + (y_{anchor_1}- y_{mote})^2 = d_{1}^2 \\
(x_{anchor_2}- x_{mote})^2 + (y_{anchor_2}- y_{mote})^2 = d_{2}^2 \\
(x_{anchor_3}- x_{mote})^2 + (y_{anchor_3}- y_{mote})^2 = d_{3}^2 \\
\end{eqnarray*}
Which rearranges to the a linear matrix equation: \\
% left / right for l/r brackets
\begin{minipage}{\textwidth}
\[2
\left[ { \begin{array}{cc}
 x_{anchor_3}-x_{anchor_1} & y_{anchor_3}-y_{anchor_1}  \\
 x_{anchor_3}-x_{anchor_2} & y_{anchor_3}-y_{anchor_2}  \\
 \end{array} } \right]
\left[ { \begin{array}{c}
 x_{mote} \\
 y_{mote} \\
 \end{array} } \right]
= \]  \[
\left[ { \begin{array}{c}
 (d_{1}^2-d_{3}^2)-(x_{anchor_1}^2-x_{anchor_3}^2)-(y_{anchor_1}^2-y_{anchor_3}^2) \\
 (d_{2}^2-d_{3}^2)-(x_{anchor_2}^2-x_{anchor_3}^2)-(y_{anchor_2}^2-y_{anchor_3}^2) \\
 \end{array} } \right]
\]
\end{minipage} \\

With well-know positions of the three anchors, only the distances are needed. As explained before this can be derived directly from the RSSI value.

%%%% *** Previous Work **** %%%%
\subsection{Previous Work}
Much research effort has been invested in performing RSSI based positioning systems \cite{A_dynamicindoor, Barsocchi_rssilocalisation}. The rather unstable nature of the RSSI value, due to effects such as absorption, reflection and fast fading, makes it a rather dubious task. \cite{Potortì_accuracylimits} studied these effects in-door and concluded that the RSSI alone is useless for positioning or localization. Figure~\ref{fig:rssi_indoor_variations} shows an example of the RSSI mapping of an office.
 \begin{figure}[!]
  \centering
  \includegraphics[width=6cm]{rssi_in_room_variations.png}
  \caption{Indoor RSSI variations from \cite{Potortì_accuracylimits}}
  \label{fig:rssi_indoor_variations}
\end{figure}
The RSSI value varies up to 50\% just by moving the transmitter around a line of equal distance to the anchor. Equal RSSI values were also found on different distances from the anchor. \cite{Barsocchi_rssilocalisation} concludes that approximately 12 anchors are needed to obtain a resolution of 150cm in an 20 sq m office. \

%%%% *** Practical Considerations about RSSI **** %%%%
\subsection{Practical Considerations about RSSI}
The Telecomunications Union ITU, specifies a general model to describe an indoor path loss \cite{itu1238-3}:
\begin{equation}
L_{total} = 20 log_{10} (f) + N log_{10} (d) + L_f(n) - 28\  [dB]
\end{equation}

Where f is the frequency in MHz, N is the distance power loss coefficient, d is separation distance and L$_{f}$ is a floor penetration factor for buildings with several floors and finally n that represents the number of floors.
The same document provides typical distance power loss coefficients (N) for different environments:

\begin{table}
\centering
\begin{tabular}{l c c c} \
Frequency & Recidencial & Office & Commercial \\
\hline
1.8-2 GHz & 28 & 30 & 22 \\
\hline
\end{tabular} 
\caption{Typical Distance Power Loss Coefficients}
\label{table:distPwrLoss}
\end{table}

The table indicates a 36 \% increase in loss going from a commercial environment to an office environment. This affects the distance estimation from eq \ref{eqn:distance} directly.
This and the indoor variations described earlier, indicates that basic tests should be performed in an environment that provides a minimum of reflections and obstacles that will affect the signal power loss coefficient. In other words if the distance measurements are to be used for positioning, then the RSSI/distance values must be calibrated in the target environment. 

%%%% ***** Measuring RSSI with the CC2420 ****** %%%%
\section{Measuring RSSI with the CC2420}\label{ch:measuringRssiCC2420}
The TelosB motes used throughout our experiments uses the CC2420 2.4GHz RF transceiver IC from Texas Instruments \cite{TelosB}. The CC2420 calculates the RSSI  over  8  symbol  periods  and  stores the result in its RSSI.RSSI\_VAL register.  Texas Instruments  specifies  the  following formula to compute the received signal power(P) in dBm:
$$P = RSSI\_VAL + RSSI\_OFFSET\ [dBm]$$ \\
The RSSI\_OFFSET of the CC2420 is found empirically during system development and is approximately -45 dBm\cite{CC2420}. As this formula indicates, the result is only an approximation and it is dependent on the RF front-end characteristics and the receiver sensitivity variations. The motes used should therefore be calibrated before use, if the power is to be used directly. 


%%%% ***** Measuring LQI with the CC2420 ****** %%%%
\section{Measuring LQI with the CC2420}
In the CC2420 the LQI  is calculated over 8 bits following the start frame delimiter (SFD). The value is calculated in the MAC layer and is partially based on the RSSI value. The CC2420 provides a ``chip error rate'' that it uses in conjunction with the RSSI value and the CRC OK/not OK, to estimate the LQI value. The LQI value read from the CC2420 ranges from 110-50 and must be converted to the 802.15.4 range of 0-255 for comparison with other wireless motes. A maximum link quality is indicated by a value of 110 and a value of 50 indicates the lowest detectable value of the CC2420 \cite{CC2420}.
